package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MxSupplier;
import com.ruoyi.system.mapper.ByParamMapper;
import com.ruoyi.system.mapper.DateUtilSqlMapper;
import com.ruoyi.system.mapper.MxSupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MxProductMapper;
import com.ruoyi.system.domain.MxProduct;
import com.ruoyi.system.service.IMxProductService;

/**
 * 商品管理Service业务层处理
 *
 * @author yushuaixm
 * @date 2024-08-14
 */
@Service
public class MxProductServiceImpl implements IMxProductService {
    @Autowired
    private MxProductMapper mxProductMapper;

    @Autowired
    private DateUtilSqlMapper dateUtilSqlMapper;

    @Autowired
    private ByParamMapper paramMapper;

    @Autowired
    private MxSupplierMapper mxSupplierMapper;


    @Override
    public List<JSONObject> getShowData() {
        return mxProductMapper.getShowData();
    }

    @Override
    public List<JSONObject> getAllProduct() {
        return mxProductMapper.getAllProduct();
    }

    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public MxProduct selectMxProductById(Long id) {
        return mxProductMapper.selectMxProductById(id);
    }

    /**
     * 查询商品管理列表
     *
     * @param mxProduct 商品管理
     * @return 商品管理
     */
    @Override
    public List<MxProduct> selectMxProductList(MxProduct mxProduct) {
        return mxProductMapper.selectMxProductList(mxProduct);
    }

    /**
     * 新增商品管理
     *
     * @param mxProduct 商品管理
     * @return 结果
     */
    @Override
    public int insertMxProduct(MxProduct mxProduct) {
        String lx = mxProduct.getLx();
        JSONObject paramEntity = paramMapper.getParamJSONObjectById(lx);
        String val = paramEntity.getString("val");
        mxProduct.setLxname(val);


        String supplier = mxProduct.getSupplier();
        JSONObject objEntity = mxSupplierMapper.allListById(supplier);
        val = objEntity.getString("value");
        mxProduct.setSupplierName(val);


        String zs = mxProduct.getZs();
        String zsName = "";
        if(!StringUtils.isEmpty(getNumberFromString(zs))){
            String[] zsStr = zs.split(",");
            for(String str:zsStr){
                String param_id = getNumberFromString(str);
                paramEntity = paramMapper.getParamJSONObjectById(param_id);
                if(paramEntity != null){
                    val = paramEntity.getString("val");
                    zsName = zsName + val+" | ";
                }
            }
        }
        mxProduct.setZsName(zsName);

        String otherSupplier = mxProduct.getOtherSupplier();
        String otherSupplierName = "";
        if(!StringUtils.isEmpty(getNumberFromString(otherSupplier))){
            String[] otherSupplierStr = otherSupplier.split(",");
            for(String str:otherSupplierStr){
                String param_id = getNumberFromString(str);
                paramEntity = mxSupplierMapper.allListById(param_id);
                if(paramEntity != null){
                    val = paramEntity.getString("value");
                    otherSupplierName = otherSupplierName + val+" | ";
                }

            }
        }
        mxProduct.setOtherSupplierName(otherSupplierName);



        mxProduct.setAddtime(dateUtilSqlMapper.getNowDateTime());
        mxProduct.setUpTime(dateUtilSqlMapper.getNowDateTime());
        return mxProductMapper.insertMxProduct(mxProduct);
    }

    /**
     * 修改商品管理
     *
     * @param mxProduct 商品管理
     * @return 结果
     */
    @Override
    public int updateMxProduct(MxProduct mxProduct) {
        String lx = mxProduct.getLx();
        JSONObject paramEntity = paramMapper.getParamJSONObjectById(lx);
        String val = paramEntity.getString("val");
        mxProduct.setLxname(val);

        String supplier = mxProduct.getSupplier();
        JSONObject objEntity = mxSupplierMapper.allListById(supplier);
        val = objEntity.getString("value");
        mxProduct.setSupplierName(val);

        String zs = mxProduct.getZs();
        String zsName = "";
        if(!StringUtils.isEmpty(getNumberFromString(zs))){
            String[] zsStr = zs.split(",");
            for(String str:zsStr){
                String param_id = getNumberFromString(str);
                paramEntity = paramMapper.getParamJSONObjectById(param_id);
                if(paramEntity != null){
                    val = paramEntity.getString("val");
                    zsName = zsName + val+" | ";
                }
            }
        }
        mxProduct.setZsName(zsName);


        String otherSupplier = mxProduct.getOtherSupplier();
        String otherSupplierName = "";
        if(!StringUtils.isEmpty(getNumberFromString(otherSupplier))){
            String[] otherSupplierStr = otherSupplier.split(",");
            for(String str:otherSupplierStr){
                String param_id = getNumberFromString(str);
                paramEntity = mxSupplierMapper.allListById(param_id);
                if(paramEntity != null){
                    val = paramEntity.getString("value");
                    otherSupplierName = otherSupplierName + val+" | ";
                }
            }
        }
        mxProduct.setOtherSupplierName(otherSupplierName);


        mxProduct.setUpTime(dateUtilSqlMapper.getNowDateTime());
        return mxProductMapper.updateMxProduct(mxProduct);
    }

    /**
     * 从 String 中提取数字
     * */
    public static String getNumberFromString(String string){
        String str = string; //"I23love234you3423java";
        str = str.trim();
        StringBuffer str2 = new StringBuffer();  // 此处可以也使用StringBuffer
        if(str != null && !"".equals(str)) {
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                    String s = String.valueOf(str.charAt(i));
                    // 为什么不用＋号拼接，当然是因为＋号拼接效率最慢的问题啊
                    str2.append(s);
                }
            }
        }
        return str2.toString();
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteMxProductByIds(Long[] ids) {
        return mxProductMapper.deleteMxProductByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteMxProductById(Long id) {
        return mxProductMapper.deleteMxProductById(id);
    }
}
